﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    Transfer
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/8 12:08:34
// Update Time         :    2012/7/8 12:08:34
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Account;
using Gfw.BLL.Basic.ExceptionImpl;
using Gfw.BLL.Basic.User.Enums;
using Gfw.Models.Account;
using Gfw.Models.Admin;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 转账，用户转用户
    /// <para>客户转用户见Recharge类</para>
    /// </summary>
    public class TransferFacility : IFacility
    {
        private UsrUser _user;
        private UsrUser _toUser;
        private double _count;

        /// <summary>
        /// 构造函数初始化
        /// </summary>
        /// <param name="user"></param>
        /// <param name="toUser"></param>
        /// <param name="count"></param>
        public TransferFacility(UsrUser user,UsrUser toUser,double count)
        {
            if(count<=0)
                throw new GfwVerifyException("转账的参数不正确！");
            _count = count;
            _toUser = toUser;
            _user = user;
        }

        /// <summary>
        /// 会员转会员
        /// </summary>
        /// <param name="unitOfWork"></param>
        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork != null)
                throw new GfwException("当前事务只能作为原子事务，不允许与其它操作公用数据库事务。");
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            ActUserAccountinformationRepository accountRep = new ActUserAccountinformationRepository();

            var userAccount = accountRep.Get(_user.Id, work);
            if (userAccount == null)
                throw new GfwVerifyException("转出用户不存在！");
            if (userAccount.ElectronicMoney < _count)
                throw new GfwVerifyException("余额不足！");
            var toUserAccount = accountRep.Get(_toUser.Id, work);
            if (toUserAccount == null)
                throw new GfwVerifyException("指定用户不存在！");

            userAccount.ElectronicMoney -= _count;
            toUserAccount.ElectronicMoney += _count;

              DateTime tradeDate = DateTime.Now;
            ActUserBargain4emoney userEhistory = new ActUserBargain4emoney()
                                                     {
                                                         Id = Guid.NewGuid().ToString(),
                                                         Userid = _user.Id,
                                                         AwardSerialNo = "",
                                                         Balance = userAccount.ElectronicMoney,
                                                         Income = 0,
                                                         Outlay = _count,
                                                         Taxation = 0,
                                                         TaxationSerialNo = "",
                                                         TradeAbstract = "转出",
                                                         TradeType = (int) UserTradeTypeEnum.Out,
                                                         TradeDate = tradeDate,
                                                         ToUserId = _toUser.Id,
                                                         ToUserLoginName = _toUser.LoginName
                                                     };
            ActUserBargain4emoney toUserEhistory = new ActUserBargain4emoney()
                                                       {
                                                           Id = Guid.NewGuid().ToString(),
                                                           Userid = _toUser.Id,
                                                           AwardSerialNo = "",
                                                           Balance = userAccount.ElectronicMoney,
                                                           Income = _count,
                                                           Outlay = 0,
                                                           Taxation = 0,
                                                           TaxationSerialNo = "",
                                                           TradeAbstract = "转入",
                                                           TradeType = (int) UserTradeTypeEnum.In,
                                                           TradeDate = tradeDate,
                                                           ToUserId = _user.Id,
                                                           ToUserLoginName = _user.LoginName
                                                       };

            accountRep.Update(userAccount, work);
            accountRep.Update(toUserAccount, work);
            ActUserBargain4emoneyRepository emoneyRep = new ActUserBargain4emoneyRepository();
            emoneyRep.Save(userEhistory, work);
            emoneyRep.Save(toUserEhistory, work);
            if (unitOfWork == null)
                work.Commit();

            //如果用户没有激活，则需要激活用户
            //激活用户，没有做强制性认证
            if (toUserAccount.Status == (int)UserStatusEnum.NotActive
                && toUserAccount.ElectronicMoney >= 560)
            {
                ActiveUserFacility active = new ActiveUserFacility(toUserAccount.Userid);
                active.Execute();
            }

        }
    }
}
